﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MapEntity;
using MapEntity.Model;
using System.IO;
using MapEntity.Services;
using MapControl;

namespace MapTileStore
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        TilesDataContext db = new TilesDataContext();

        TileType _tileType;
        
        private string mouseRightClickLocation = string.Empty;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void map_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var loc = e.GetPosition(map);
            mouseRightClickLocation = map.ViewportPointToLocation(loc).ToString();
        }

        private void copyLocationToClipboard_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetData(DataFormats.Text, (object)mouseRightClickLocation);
        }

        private void MapManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
        {
            e.TranslationBehavior.DesiredDeceleration = 0.001;
        }

        private void MapMouseLeave(object sender, MouseEventArgs e)
        {
            mouseLocation.Text = string.Empty;
        }

        private void MapMouseMove(object sender, MouseEventArgs e)
        {
            mouseLocation.Text = map.ViewportPointToLocation(e.GetPosition(map)).ToString();
        }

        private void SeamarksClick(object sender, RoutedEventArgs e)
        {
            TileLayer seamarks = (TileLayer)Resources["SeamarksTileLayer"];
            CheckBox checkBox = (CheckBox)sender;

            if ((bool)checkBox.IsChecked)
            {
                map.TileLayers.Add(seamarks);
            }
            else
            {
                map.TileLayers.Remove(seamarks);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            comboBoxProvider.ItemsSource = db.Providers.Select(p=>p).ToList();

            _tileType = new TileType()
            {
                Longitude = 50.93684,
                Latitude = 35.52214,
                ZoomLevel = 16
            };
            formGrid.DataContext = _tileType;
        }

        public static ImageSource ByteToImage(byte[] imageData)
        {
            if (imageData == null)
                return null;

            BitmapImage biImg = new BitmapImage();
            MemoryStream ms = new MemoryStream(imageData);
            biImg.BeginInit();
            biImg.StreamSource = ms;
            biImg.EndInit();

            ImageSource imgSrc = biImg as ImageSource;

            return imgSrc;
        }

        //private void buttonShowLocation_Click(object sender, RoutedEventArgs e)
        //{
        //    var provider = comboBoxProvider.SelectedItem as Provider;

        //    if (provider == null)
        //        return;

        //    if (_tileType.ZoomLevel < provider.MinZoomLevel || _tileType.ZoomLevel > provider.MaxZoomLevel)
        //    {
        //        return;
        //    }

        //    TileService tileService = new TileService();

        //    Tile tile = tileService.GetTile(_tileType);

        //    if (tile != null)
        //    {
        //        if (tile.Date <= expireDatePiker.SelectedDate)
        //        {
        //            _tileType.Image = tile.ImageTileSource.Image;
        //            image.Source = ByteToImage(_tileType.Image);

        //            return;
        //        }
        //    }

        //    MapApi mapApi = new MapApi(_tileType.ZoomLevel);

        //    TileSource tileSource = new TileSource(provider.TileSource);

        //    _tileType.Image = TileLoad.DownloadTile(tileSource.GetUri(_tileType.X, _tileType.Y, _tileType.ZoomLevel));

        //    if (_tileType.Image == null)
        //        return;

        //    if (_tileType.Image.Length > 0)
        //    {
        //        image.Source = ByteToImage(_tileType.Image);

        //        tileService.AddOrUpdateTile(_tileType);
        //    }
        //}
    }
}
